<!--

    Copyright 2006-2019 the original author or authors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<html>
<head>
    <title>Twenty Second Tutorial</title>
</head>
<body>

<p>There are two main interfaces in Objenesis:</p>

<ul>
    <li><strong><a href="${javadoc}/org/objenesis/instantiator/ObjectInstantiator.html">ObjectInstantiator</a></strong>
        - Instantiates multiple instances of a single class.
        <pre>interface ObjectInstantiator {
  Object newInstance();
}</pre>
    </li>
    <li><strong><a href="${javadoc}/org/objenesis/strategy/InstantiatorStrategy.html">InstantiatorStrategy</a></strong>
        - A particular strategy for how to instantiate a class (as this differs for different types of classes).
        <pre>interface InstantiatorStrategy {
  ObjectInstantiator newInstantiatorOf(Class type);
}</pre>
    </li>
</ul>

<p><strong>Note:</strong> All Objenesis classes are in the
    <strong><code>org.objenesis</code></strong> package.</p>

<h1>Step By Step</h1>

<p>There are many different strategies that Objenesis uses for instantiating objects based on the JVM vendor,
JVM version, SecurityManager and type of class being instantiated.</p>

<p>We have defined that two different kinds of instantiation are required:</p>

<ul>
    <li><strong>Stardard</strong> - No constructor will be called</li>
    <li><strong>Serializable compliant</strong> - Acts like an object instantiated by java standard
       serialization. It means that the constructor of the first non-serializable parent class will
       be called. However, specific implementation methods (e.g. <code>readResolve()</code>) are not called and we never check if the object is serializable.</li>
</ul>

<p>The simplest way to use Objenesis is by using <a href="${javadoc}/org/objenesis/ObjenesisStd.html">ObjenesisStd</a> (Standard) and
<a href="${javadoc}/org/objenesis/ObjenesisSerializer.html">ObjenesisSerializer</a> (Serializable compliant). By default,
automatically determines the best strategy - so you don't have to.</p>

<pre>Objenesis objenesis = new ObjenesisStd(); // or ObjenesisSerializer</pre>

<p>Once you have the Objenesis implementation, you can then create an <code>ObjectInstantiator</code>, for a specific type.</p>

<pre>ObjectInstantiator thingyInstantiator = objenesis.getInstantiatorOf(MyThingy.class);</pre>

<p>Finally, you can use this to instantiate new instances of this type.</p>

<pre>MyThingy thingy1 = (MyThingy)thingyInstantiator.newInstance();
MyThingy thingy2 = (MyThingy)thingyInstantiator.newInstance();
MyThingy thingy3 = (MyThingy)thingyInstantiator.newInstance();</pre>

<h1>Performance and Threading</h1>

<p>To improve performance, it is best to reuse the <code>ObjectInstantiator</code>
    objects as much as possible. For example, if you are instantiating multiple instances of a specific class,
    do it from the same <code>ObjectInstantiator</code>.
</p>

<p>Both <code>InstantiatorStrategy</code> and <code>ObjectInstantiator</code> can be shared between multiple
    threads and used concurrently. They are thread safe.</p>

<h1>That Code Again</h1>

<p>(For the impatient)</p>

<pre>
Objenesis objenesis = new ObjenesisStd(); // or ObjenesisSerializer
MyThingy thingy1 = (MyThingy) objenesis.newInstance(MyThingy.class);

// or (a little bit more efficient if you need to create many objects)

Objenesis objenesis = new ObjenesisStd(); // or ObjenesisSerializer
ObjectInstantiator thingyInstantiator = objenesis.getInstantiatorOf(MyThingy.class);

MyThingy thingy2 = (MyThingy)thingyInstantiator.newInstance();
MyThingy thingy3 = (MyThingy)thingyInstantiator.newInstance();
MyThingy thingy4 = (MyThingy)thingyInstantiator.newInstance();
</pre>

</body>
</html>
